//
//  IGMyiGalleryVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/23.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGMyiGalleryVC.h"
#import "IGiGallerySetVC.h"
#import "AddDvViewController.h"
#import "IGTwoCodeVC.h"
#import "IGiGalleryListApi.h"
#import "IGiGalleryDSlaveEquApi.h"
#import "IGWebViewController.h"

@interface IGMyiGalleryVC ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tableHeadView;
@property (nonatomic, strong) IGiGalleryListApi *iGalleryListapi;
@property (nonatomic, strong) IGiGalleryDSlaveEquApi *iGalleryDSlaveEquApi;
    
@property (nonatomic, strong) NSMutableArray *iGalleryListArr;

@end

@implementation IGMyiGalleryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"我的画屏";
    self.title = NSLocalizedString(@"My iGallery", nil);
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    [self ig_setupRightButtonItemWithTitle:nil image:[UIImage imageNamed:@"help_icon"] highLightedImage:[UIImage imageNamed:@"help_icon"]];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
    
- (void)requestData {
    self.iGalleryListapi = [[IGiGalleryListApi alloc] initWithType:IGiGalleryListTypeAll];
    
    __weak typeof(self) weakSelf = self;
    [self.iGalleryListapi startWithDecodeClass:[IGiGalleryListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGiGalleryListResp *resp = responseObject;
            IGiGalleryListModel *model = resp.data;
            weakSelf.iGalleryListArr = [NSMutableArray arrayWithArray:model.equs];
            
            [weakSelf.tableView reloadData];
        }
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.tableHeaderView = self.tableHeadView;
        _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
    }
    return _tableView;
}

- (UIView *)tableHeadView {
    if (!_tableHeadView) {
        _tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 85)];
        _tableHeadView.backgroundColor = [UIColor mainBackgroundColor];
        
        UIButton *addiGalleryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tableHeadView addSubview:addiGalleryBtn];
        [addiGalleryBtn setFrame:CGRectMake(40, 20, KSCREENWIDTH - 80, 44)];
        [addiGalleryBtn setImage:[UIImage imageNamed:@"iGallery_add"] forState:UIControlStateNormal];
//        [addiGalleryBtn setTitle:@"添加画屏" forState:UIControlStateNormal];
        [addiGalleryBtn setTitle:NSLocalizedString(@"Add iGallery", nil) forState:UIControlStateNormal];

        [addiGalleryBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [addiGalleryBtn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
        addiGalleryBtn.backgroundColor = [UIColor c3Color];
        addiGalleryBtn.layer.masksToBounds = YES;
        addiGalleryBtn.layer.cornerRadius = 22;
        [addiGalleryBtn setTapActionWithBlock:^{
            [UIViewController ig_pushViewControllerClass:[IGTwoCodeVC class] animated:YES];
        }];
    }
    return _tableHeadView;
}

- (void)addiGallery {
    [UIViewController ig_pushViewControllerClass:[IGTwoCodeVC class] animated:YES];
}

#pragma mark -<UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.iGalleryListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"IGMyiGalleryVCCellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    IGiGalleryEqu *equ = self.iGalleryListArr[indexPath.row];
    cell.textLabel.font = [UIFont font16];
    cell.textLabel.textColor = [UIColor c4Color];
    cell.detailTextLabel.textColor = [UIColor c5Color];
    cell.detailTextLabel.font = [UIFont font12];
    
    cell.textLabel.text = equ.title;
    if ([equ.type intValue] == 1) {//主绑
        cell.imageView.image = [UIImage imageNamed:@"iGallery_Bind_Master"];
        cell.detailTextLabel.text = @"";
    } else if([equ.type intValue] == 2) {//从绑
        
        if([equ.status intValue] == 1) {
            cell.imageView.image = [UIImage imageNamed:@"iGallery_Bind_Slave"];
            cell.detailTextLabel.text = @"";
        } else if([equ.status intValue] == 2) {//从绑-等待用户同意
            cell.imageView.image = [UIImage imageNamed:@"iGallery_Bind_Slave1"];
//            cell.textLabel.text = @"等待同意画屏";
//            cell.detailTextLabel.text = @"等待主绑用户同意";
            cell.textLabel.text = NSLocalizedString(@"Wait to Approve BOE-iGallery", nil);
//            cell.detailTextLabel.text = @"等待主绑用户同意";
            cell.detailTextLabel.text = NSLocalizedString(@"Waiting for Approval of Main Binding User", nil);
        } else if([equ.status intValue] == 3) {//从绑-用户拒绝
            cell.imageView.image = [UIImage imageNamed:@"iGallery_Bind_Slave1"];
//            cell.textLabel.text = @"拒绝同意画屏";
            cell.textLabel.text = NSLocalizedString(@"Deny to Approve BOE-iGallery", nil);
//            cell.detailTextLabel.text = @"主绑用户拒绝了你的绑定";
            cell.detailTextLabel.text = NSLocalizedString(@"Your binding is denied by main binding user.", nil);
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IGiGalleryEqu *equ = self.iGalleryListArr[indexPath.row];
    if ([equ.type intValue] == 1) {//主绑
        IGiGallerySetVC *setVC = [[IGiGallerySetVC alloc] initWithType:IGiGalleryBindingTypeMain equ:equ];
        [UIViewController ig_pushViewController:setVC animated:YES];
    } else if([equ.type intValue] == 2) {//从绑
        if([equ.status intValue] == 1) {
            IGiGallerySetVC *setVC = [[IGiGallerySetVC alloc] initWithType:IGiGalleryBindingTypeSlave equ:equ];
            [UIViewController ig_pushViewController:setVC animated:YES];
        } else if([equ.status intValue] == 2) {//从绑-等待用户同意
            IGiGallerySetVC *setVC = [[IGiGallerySetVC alloc] initWithType:IGiGalleryBindingTypeSlaveWait equ:equ];
            [UIViewController ig_pushViewController:setVC animated:YES];
        } else if([equ.status intValue] == 3) {//从绑-用户拒绝
            
        }
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGiGalleryEqu *equ = self.iGalleryListArr[indexPath.row];
    if ([equ.type intValue] == 2 && ([equ.status intValue] == 2 || [equ.status intValue] == 3)) {
        return YES;
    }
    return NO;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return @"删除";
    return NSLocalizedString(@"Delete", nil);
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    IGiGalleryEqu *equ = self.iGalleryListArr[indexPath.row];
    // 从数据源中删除
    [self.iGalleryListArr removeObjectAtIndex:indexPath.row];
    // 从列表中删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
    [self iGalleryDSlaveEqu:equ indexPath:indexPath];
}

- (void)iGalleryDSlaveEqu:(IGiGalleryEqu *)equ indexPath:(NSIndexPath *)indexPath {
    _iGalleryDSlaveEquApi = [[IGiGalleryDSlaveEquApi alloc] initWithMacId:equ.macId slaveUid:[IGUser getUid] type:IGiGalleryDeleteSlaveEquTypeReject];
    if ([equ.status isEqualToString:@"2"]) {
        _iGalleryDSlaveEquApi.typeRejecttype = @"2";
    } else if ([equ.status isEqualToString:@"3"]) {
        _iGalleryDSlaveEquApi.typeRejecttype = @"1";
    }
    [_iGalleryDSlaveEquApi startWithDecodeClass:[IGiGalleryDSlaveEquResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            
        } else {//删除失败恢复
            [self.iGalleryListArr insertObject:equ atIndex:indexPath.row];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }];
}
    
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}

#pragma mark -Empty

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"你还没有添加画屏哦~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No iGallery Added", nil) attributes:nil];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"igallery"];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    [UIViewController ig_pushViewControllerClass:[IGTwoCodeVC class] animated:YES];
}

- (void)ig_rightButtonPressed:(id)sender {
    IGWebViewController *vc = [[IGWebViewController alloc] initWithType:IGWebViewTypeFive];
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
